package com.springboot.controller;

import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorSendMessageService;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 本控制台用于医生提醒患者就诊
 * 给该患者发送信息
 */
@Controller
public class DoctorSendMessageController {
    @Autowired
    DoctorSendMessageService doctorSendMessageService;
    /**
     * 医生提醒患者就诊
     */
    @CrossOrigin
    @RequestMapping(value = "/sendMessageServlet")
    public @ResponseBody
    String doctorSendMessage(@RequestParam("doctorTel") String doctorTel,
                             @RequestParam("patientTel") String patientTel,
                             @RequestParam("bookDate") String bookDate,
                             @RequestParam("bookTime") String bookTime) {
        System.out.println("医生提醒患者就诊");
        try {
            UserLoginForm doctor=doctorSendMessageService.findUserByTel(doctorTel);
            UserLoginForm patient=doctorSendMessageService.findUserByTel(patientTel);
            String doctorName=doctor.getUserName();
            String patientName=patient.getUserName();
            ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "106819", "ZWRhMWQ4MTgtZDMyNS00ZjM3LTg1NGYtMGVkOTU5NDUyOGZj");
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number",patientTel);
            params.put("templateId", "2563"); //2563短信模板
            /**
             * String[] templateParams = new String[4];
             *             templateParams[0] = patientName;
             *             templateParams[1] = doctorName;
             *             templateParams[2] = bookDate;
             *             templateParams[3] = bookTime;
             *             params.put("templateParams", templateParams);
             */
            String result = null;
            try {
                result = client.send(params);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String balance = null;
            try {
                balance = client.balance();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("result = " + result);
            System.out.println("balance = " + balance);
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
